package com.flashphoner.api.management
{
	import flash.media.Microphone;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundCodec;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class SoundControl
	{
		
		private static var soundControl:SoundControl;
		
		private var mic:Microphone;
		
		public function SoundControl()
		{
			mic = Microphone.getMicrophone();		
			mic.codec = SoundCodec.SPEEX;
			mic.framesPerPacket = 1;
			mic.rate = 16;
			mic.gain = 50;
			mic.encodeQuality = 6;
			mic.soundTransform = new SoundTransform(1,0);			
			mic.setLoopBack(false);			
			mic.setSilenceLevel(0,3600000);
			mic.setUseEchoSuppression(true);
		}
		
		
		public static function getInstance():SoundControl{
			if (soundControl == null){
				soundControl = new SoundControl();
			}
			return soundControl;
		}
	    

		public function changeMicrophone(microphone:Microphone,isLoopback:Boolean,gain:Number = -1):void{
			var oldGain:int = this.mic.gain;
			this.mic = microphone;
			if (this.mic != null){
				mic.codec = SoundCodec.SPEEX;
				mic.framesPerPacket = 1;
				mic.rate = 16;
				if (gain != -1){
					mic.gain = gain;
				}else{
					mic.gain = oldGain;
				}
				mic.encodeQuality = 6;
				mic.soundTransform = new SoundTransform(1,0);			
				mic.setLoopBack(isLoopback);			
				mic.setSilenceLevel(0,3600000);
				mic.setUseEchoSuppression(true);
			}
		}
		
		public function getMicrophone():Microphone{
			return mic;
		}		
		

	}
}